//两秒后消失的提示框
function alert2000($target, info, model = 1) {
    //target为警告的外框，info为html里面的信息
    //1为页面主体的警告框，2为模态框中的警告框
    if (model == 1) {
        var mes = backalert + info + "</div>";
        $target.html(mes).fadeIn();
        setTimeout(function() {
            $target.fadeOut("slow", function() {
                $(this).html("");
            });
        }, 2000);
    } else if (model == 2) {
        $target.html(info).fadeIn();
        setTimeout(function() {
            $target.fadeOut("slow", function() {
                $target.html("");
            });
        }, 800);
    }

}
//永不消逝的一维json对象遍历提示框
function alertarray($target, datas) {
    var temp;
    $.each(datas, function(i, v) {
        temp += i + ":" + v + "<br>";
    })
    temp = backalert + temp + "</div>";
    $target.html(temp).fadeIn();
}
//永不消失的json转字符串提示框
function alertjts($target, data) {
    var temp = JSON.stringify(data);
    temp = backalert + temp + "</div>";
    $target.html(temp).fadeIn();
}
//知识点名称验证
function kname_vel(kname, model = 0, url = '', $alerttarget = '') {
    var info = '';
    try {
        if (model == 0) {
            if (kname == "") {
                info = "名称不能为空";
            } else {
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: url,
                    async: false, //$.ajax因为异步的原因不能赋值，改成同步就行了
                    data: { kname: kname },
                    beforeSend: function() {
                        alert2000($alerttarget, "正在验证名称是否重复……", 1);
                    },
                    success: function(data) {
                        if (data.num !== '0') {
                            info = "名称已经存在";
                        }

                    }
                })
            }
            if (info !== '') {
                throw info;
            }
        }
        if (kname.length > 20) {
            info = "名称长度不能大于20字符";
            throw info;

        }
    } catch (e) {
        return e;
    }
    return false;
}
//知识点标签验证
function klabel_vel(klabel, model = 0) {
    var info;
    var result = klabel.split(",");
    try {

        if (model == 0) {
            if (klabel == "") {
                info = "标签不能为空";
                throw info;
            }
            if (result.length > 5) {
                info = "标签不得多于5个";
                throw info;
            }
            for (var i in result) {
                if (result[i].length > 14) {
                    info = "每个标签的长度不能大于14字符";
                    throw info;
                }
                if (result[i] == "") {
                    info = "每个标签都不能为空";
                    throw info;
                }
            }
        }else if (model ==2){
            if (klabel == "") {
                info = "标签不能为空";
                throw info;
            }
            if (result.length > 1) {
                info = "一次只能设置一个标签";
                throw info;
            }
        } else {
            if (result.length > 1) {
                info = "一次只能查询一个标签";
                throw info;
            }
        }
    } catch (e) {
        return e;
    }
    return false;
}
//时间验证
function ktime_vel(ftime) {
    var info;
    try {
        if (ftime == "") {
            info = "必需选择时间！";
            throw info;
        }
    } catch (e) {
        return e;
    }
    return false;
}
//知识点详情验证
function kdetail_vel(kdetail) {
    var info;
    try {
        if (kdetail == "") {
            info = "请填写知识点详情!";
            throw info;
        }
    } catch (e) {
        return e;
    }
    return false;
}
//知识点自动填充
function kdetail_autoget(kdetail) {
    kdetail = replaceBlank(kdetail);
    var reg = /[a-z]{0,5}\./;
    var all = kdetail.split('\n');
    var cixing = new Array();
    //压入词性
    for(var i =1;i<all.length;i++){
        if(reg.test(all[i])){
            cixing.push(all[i]);
        }
    }
    var reslut = {kname:all.splice(0,1)[0],kdetail:all.join('\n'),klabel:cixing.join(',')};
    return reslut;
}
//去掉空行函数
function replaceBlank(str) {
    var reg = /\n(\n)*( )*(\n)*\n/g;
    return str.replace(reg, "\n");
}
//正则表达式获取url参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
//排序函数
function orderByField($target, sort, $alerttarget, model = 1) {
    $target.on("click", function() {
        var args = {};
        orderIconChange($target);
        if (model == 1) {
            if (args = getArgs($alerttarget)) {
                args.order = $target.data("order");
                args.sort = sort;
                getData($alerttarget, args);
            }
        } else if (model == 2) {
            if (args = getArgsForView($alerttarget)) {
                args.order = $target.data("order");
                args.sort = sort;
                getDataForView($alerttarget, args);
            }
        }

    })
}
//排序图标变化函数
function orderIconChange($target) {
    var order = $target.data("order");
    if (order == "ASC") {
        $target.children("span").removeClass("glyphicon-triangle-bottom icon-green").addClass("glyphicon-triangle-top icon-red");
        $target.data("order", "DESC");
    } else {
        $target.children("span").removeClass("glyphicon-minus glyphicon-triangle-top icon-red").addClass("glyphicon-triangle-bottom icon-green");
        $target.data("order", "ASC");
    }
}
//knowledgelist页面获取参数
function getArgs($oandstarget) {
    var args = {
        page: "1",
        kname: $("#k-l-name").val(),
        label: $("#k-l-label").val(),
        complate: $("input[name=k-l-complate]:checked").val(),
        pass: $("input[name=k-l-pass]:checked").val(),
        starttime: $("#k-l-start-time").val(),
        endtime: $("#k-l-end-time").val()
    }
    var idinfo;
    try {
        if (idinfo = kname_vel(args.kname, 1)) {
            throw idinfo;
        };
        if (idinfo = klabel_vel(args.label, 1)) {
            throw idinfo;
        };
        // if(args.kname == "" && args.label == "" && args.complate == undefined && args.pass == undefined && args.starttime == "" && args.endtime == "") {
        //  throw "至少要选择一项";
        // }
    } catch (e) {
        alert2000($oandstarget, e);
        return false;
    }
    return args;
}
//knowledgelist页面接数据
function getData($alerttarget, args) {
    var tempdata = {};
    if (typeof args.page == "string") {
        tempdata.page = args.page;
    }
    if (typeof args.sort == "string") {
        tempdata.sort = args.sort;
    }
    if (typeof args.order == "string") {
        tempdata.order = args.order;
    }
    if (typeof args.kname == "string" && args.kname != "") {
        tempdata.kname = args.kname;
    }
    if (typeof args.label == "string" && args.label != "") {
        tempdata.label = args.label;
    }
    if (typeof args.pass == "string") {
        tempdata.pass = args.pass;
    }
    if (typeof args.complate == "string") {
        tempdata.complate = args.complate;
    }
    if (typeof args.starttime == "string" && args.starttime != "") {
        tempdata.starttime = args.starttime;
    }
    if (typeof args.endtime == "string" && args.endtime != "") {
        tempdata.endtime = args.endtime;
    }
    $(".td-loading").css("display", "table-cell");
    $(".tr-all").remove();
    //alertjts($alerttarget,tempdata);
    //alert(typeof tempdata.pass);
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "",
        data: tempdata,
        beforeSend: function() {
            alert2000($alerttarget, "数据正在加载……", 1);
        },
        success: function(data) {
            $alerttarget.css("display", "none");
            $(".td-loading").css("display", "none");
            var allinfo;
            if (data.knowledges.length == 0) {
                allinfo = '<tr class = "tr-all"><td colspan = "6">未查询到结果！</td></tr>';
                $("#main-table").append(allinfo);
                return false;
            }
            $.each(data.knowledges, function(i, v) {
                var iscomplate = v.iscomplate == 8 ? "结束" : "未结束";
                var ispass = v.ispass == 8 ? "通过" : "未通过";
                allinfo = '<tr ' +
                    'class="tr-' + v.kid + ' tr-all" ' +
                    'data-kid="' + v.kid + '"' +
                    'data-kname="' + v.kname + '"' +
                    'data-kdetail="' + v.kdetail + '">' +
                    '<td>' + v.kname + '</td>' +
                    '<td>' + v.ftime + '</td>' +
                    '<td class="viewreminderandcomplate" title="点击出现提醒时间和完成状态详情">提醒时间和完成状态<span class="glyphicon glyphicon-triangle-bottom little-icon" aria-hidden="true"></span></td>' +
                    '<td>' + v.iscomplate + '</td>' +
                    '<td class="iscomplate-' + v.kid + '">' + iscomplate + '</td>' +
                    '<td>' + v.ispass + '</td>' +
                    '<td class="ispass-' + v.kid + '">' + ispass + '</td><td>' +
                    '<span class="glyphicon glyphicon-repeat little-icon icon-green k-l-repeat" aria-hidden="true" title="重新将知识点加入复习计划"></span>' +
                    '<span class="glyphicon glyphicon-pencil little-icon icon-blue k-l-edit" aria-hidden="true" title="编辑知识点"></span>' +
                    '<span class="glyphicon glyphicon-remove little-icon icon-red k-l-remove" aria-hidden="true" title="删除知识点"></span></td></tr>'
                $("#main-table").append(allinfo);
            })
            $(".pagination-html").html(data.pagination); //
        }
    }) //ajax
}

function getArgsForView($alerttarget) {
    var args = {
        page: "1",
        kname: $("#k-v-name").val(),
        label: $("#k-v-label").val(),
        star: $("#view-complate-star").val(),
        times: $("#view-review-times").val(),
        todaystarttime: $("#k-v-start-time").val(),
        todayendtime: $("#k-v-end-time").val()
    }
    var idinfo;
    try {
        if (idinfo = kname_vel(args.kname, 1)) {
            throw idinfo;
        };
        if (idinfo = klabel_vel(args.label, 1)) {
            throw idinfo;
        };
        // if(args.kname == "" && args.label == "" && args.complate == undefined && args.pass == undefined && args.starttime == "" && args.endtime == "") {
        //  throw "至少要选择一项";
        // }
    } catch (e) {
        alert2000($alerttarget, e);
        return false;
    }
    return args;
}
//viewreminder页面接数据
function getDataForView($alerttarget, args) {
    var tempdata = {};
    if (typeof args.page == "string") {
        tempdata.page = args.page;
    }
    if (typeof args.sort == "string") {
        tempdata.sort = args.sort;
    }
    if (typeof args.order == "string") {
        tempdata.order = args.order;
    }
    if (typeof args.kname == "string" && args.kname != "") {
        tempdata.kname = args.kname;
    }
    if (typeof args.label == "string" && args.label != "") {
        tempdata.label = args.label;
    }
    if (typeof args.star == "string" && args.star != "") {
        tempdata.star = args.star;
    }
    if (typeof args.times == "string" && args.times != "") {
        tempdata.times = args.times;
    }
    if (typeof args.todaystarttime == "string" && args.todaystarttime != "") {
        tempdata.todaystarttime = args.todaystarttime;
    }
    if (typeof args.todayendtime == "string" && args.todayendtime != "") {
        tempdata.todayendtime = args.todayendtime;
    }
    $(".td-loading").css("display", "table-cell");
    $(".tr-all").remove();
    //alertjts($alerttarget,tempdata);
    //alert(typeof tempdata.pass);
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "",
        data: tempdata,
        beforeSend: function() {
            alert2000($alerttarget, "数据正在加载……", 1);
        },
        success: function(data) {
            $alerttarget.css("display", "none");
            $(".td-loading").css("display", "none");
            var allinfo;
            if (data.knowledges.length == 0) {
                allinfo = '<tr class = "tr-all"><td colspan = "5">未查询到结果！</td></tr>';
                $(".table-viewreminder").append(allinfo);
                return false;
            }
            $.each(data.knowledges, function(i, v) {
                var starhtml = '';
                if (v.star == 0) {
                    starhtml = '<span class="glyphicon glyphicon-star-empty star little-icon" aria-hidden="true"></span>' +
                        '<span class="glyphicon glyphicon-star-empty star little-icon" aria-hidden="true"></span>' +
                        '<span class="glyphicon glyphicon-star-empty star little-icon" aria-hidden="true"></span>' +
                        '<span class="glyphicon glyphicon-star-empty star little-icon" aria-hidden="true"></span>' +
                        '<span class="glyphicon glyphicon-star-empty star little-icon" aria-hidden="true"></span>';
                } else {
                    for (var i = 0; i < v.star; i++) {
                        starhtml += '<span class="glyphicon glyphicon-star icon-margin" aria-hidden="true"></span>';
                    }
                }
                allinfo = '<tr class="view-tr tr-all">' +
                    '<td>' + v.kname + '</td>' +
                    '<td>第' + v.times + '次</td>' +
                    '<td>' + v.date + '</td>' +
                    '<td><span class="glyphicon glyphicon-list-alt icon-green little-icon view-detail-icon" aria-hidden="true" data-detail="' + v.kdetail + '" data-kname="' + v.kname + '"></td>' +
                    '<td class="star-td-c star-td-' + v.cid + '" data-cid="' + v.cid + '" data-kname="' + v.kanme + '" data-times="' + v.times + '">' +
                    starhtml + '</td></tr>';
                $(".table-viewreminder").append(allinfo);
            })
            $(".pagination-html").html(data.pagination); //
        }
    }) //ajax
}
//获取当前时间
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
        " " + date.getHours() + seperator2 + date.getMinutes() +
        seperator2 + date.getSeconds();
    return currentdate;
}
